#include "EditorPanelViewModel.h"
#include "EditorPanelModel.h"
#include "Ability/EditorAbilityModel.h"
#include "Ability/EditorAbilityViewModel.h"

#include "Asset/EditorDefaultPackage.h"

#include "Framework/Editor.h"

void REditorPanelViewModel::BindModel(TSharedObjectPtr<REditorPanelModel> InModel)
{
    Model=InModel;

    PanelCloseIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconClose_Highlight");

    PanelAbilitys.Reserve(Model->GetPanelAbilitysNum());
    for(int64_t i = 0; i < Model->GetPanelAbilitysNum() ; i++)
    {
        auto AbilityModel = Model->GetPanelAbility(i);
        
        auto ViewModelClass = REditor::Get()->GetViewModelClass(AbilityModel->GetClass());
        auto AbilityViewModel = NewObject<REditorAbilityViewModel>(ViewModelClass, this);
        AbilityViewModel->BindModel(AbilityModel);
        PanelAbilitys.Add(AbilityViewModel);
    }

    Model->OnPanelAbilityAdded.Bind([this](int64_t Index, TSharedObjectPtr<REditorAbilityModel> InAbility)
    {
        auto ViewModelClass = REditor::Get()->GetViewModelClass(InAbility->GetClass());
        auto AbilityViewModel = NewObject<REditorAbilityViewModel>(ViewModelClass, this);
        AbilityViewModel->BindModel(InAbility);
        PanelAbilitys.Insert(Index, AbilityViewModel);
        OnPanelAbilityAdded.ExecuteIfBound(Index,AbilityViewModel);
    });

    Model->OnPanelAbilityRemoved.Bind([this](int64_t Index)
    {
        PanelAbilitys.RemoveAt(Index);
        OnPanelAbilityRemoved.ExecuteIfBound(Index);
    });
}


bool REditorPanelViewModel::IsPanelAbilityVisible(int64_t InIndex) const
{
    return Model->ActivedAbilityIndex==InIndex;
}

void REditorPanelViewModel::SetActivedPanelAbilityIndex(int64_t InIndex)
{
    Model->ActivedAbilityIndex = InIndex;
}

int64_t REditorPanelViewModel::GetPanelAbilitysNum() const
{
    return PanelAbilitys.Num();
}

TSharedObjectPtr<REditorAbilityViewModel> REditorPanelViewModel::GetPanelAbility(int64_t Index) const
{
    return PanelAbilitys[Index];
}